package cn.usian.controller;

import cn.usian.common.R;
import cn.usian.entity.Dish;
import cn.usian.entity.DishDTO;
import cn.usian.service.DishFlavorService;
import cn.usian.service.DishService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("dish")
public class DishController {
    @Resource
    private DishService dishService;

    @Resource
    private DishFlavorService dishFlavorService;

    @GetMapping("page")
    public R findPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10")Integer pageSize, String name){

        Page<DishDTO> page1 = new Page<>(page,pageSize);
        return dishService.findPage(page1,name);
    }



    @PostMapping("status/{status}")
    public R updateone(Long[] ids,@PathVariable("status") Integer status){
        dishService.updateone(ids,status);
        return  R.success("状态修改成功");
    }

    @DeleteMapping
    public R delDish(Long[]ids){
        return dishService.delDish(ids);
    }

    @PostMapping
    public R addDish(@RequestBody DishDTO dishDTO, HttpServletRequest request){
        Long id=(Long)request.getSession().getAttribute("employee");
        dishDTO.setCreateUser(id);
        dishDTO.setUpdateUser(id);
        request.getSession().setAttribute("dishId",dishDTO.getId());
        return dishService.addDish(dishDTO);
    }

    @GetMapping("list")
    public R list(Dish dish){
        return dishService.list(dish);
    }
    @GetMapping("{id}")
    public R findById(@PathVariable Long id){
        return dishService.findById(id);
    }
    @PutMapping
    public R updateDish(@RequestBody DishDTO dishDTO, HttpServletRequest request){
        Long id=(Long)request.getSession().getAttribute("employee");
        dishDTO.setCreateUser(id);
        dishDTO.setUpdateUser(id);
        return dishService.updateDish(dishDTO);
    }
}
